<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="所属楼层id" prop="floorId">
      <el-input v-model="dataForm.floorId" placeholder="所属楼层id"></el-input>
    </el-form-item>
    <el-form-item label="所属楼栋id" prop="buildingId">
      <el-input v-model="dataForm.buildingId" placeholder="所属楼栋id"></el-input>
    </el-form-item>
    <el-form-item label="业主id" prop="ownerId">
      <el-input v-model="dataForm.ownerId" placeholder="业主id"></el-input>
    </el-form-item>
    <el-form-item label="安全检查员id" prop="safetyInspectorId">
      <el-input v-model="dataForm.safetyInspectorId" placeholder="安全检查员id"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="灭火器状态" prop="fireExtinguisherStatus">
      <el-input v-model="dataForm.fireExtinguisherStatus" placeholder="灭火器状态"></el-input>
    </el-form-item>
    <el-form-item label="烟雾检测器状态" prop="smokeDetectorStatus">
      <el-input v-model="dataForm.smokeDetectorStatus" placeholder="烟雾检测器状态"></el-input>
    </el-form-item>
    <el-form-item label="楼梯杂物情况" prop="stairMess">
      <el-input v-model="dataForm.stairMess" placeholder="楼梯杂物情况"></el-input>
    </el-form-item>
    <el-form-item label="燃气情况" prop="gasStatus">
      <el-input v-model="dataForm.gasStatus" placeholder="燃气情况"></el-input>
    </el-form-item>
    <el-form-item label="电力系统情况" prop="electricity">
      <el-input v-model="dataForm.electricity" placeholder="电力系统情况"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          floorId: '',
          buildingId: '',
          ownerId: '',
          safetyInspectorId: '',
          address: '',
          fireExtinguisherStatus: '',
          smokeDetectorStatus: '',
          stairMess: '',
          gasStatus: '',
          electricity: ''
        },
        dataRule: {
          floorId: [
            { required: true, message: '所属楼层id不能为空', trigger: 'blur' }
          ],
          buildingId: [
            { required: true, message: '所属楼栋id不能为空', trigger: 'blur' }
          ],
          ownerId: [
            { required: true, message: '业主id不能为空', trigger: 'blur' }
          ],
          safetyInspectorId: [
            { required: true, message: '安全检查员id不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          fireExtinguisherStatus: [
            { required: true, message: '灭火器状态不能为空', trigger: 'blur' }
          ],
          smokeDetectorStatus: [
            { required: true, message: '烟雾检测器状态不能为空', trigger: 'blur' }
          ],
          stairMess: [
            { required: true, message: '楼梯杂物情况不能为空', trigger: 'blur' }
          ],
          gasStatus: [
            { required: true, message: '燃气情况不能为空', trigger: 'blur' }
          ],
          electricity: [
            { required: true, message: '电力系统情况不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/house/house/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.floorId = data.house.floorId
                this.dataForm.buildingId = data.house.buildingId
                this.dataForm.ownerId = data.house.ownerId
                this.dataForm.safetyInspectorId = data.house.safetyInspectorId
                this.dataForm.address = data.house.address
                this.dataForm.fireExtinguisherStatus = data.house.fireExtinguisherStatus
                this.dataForm.smokeDetectorStatus = data.house.smokeDetectorStatus
                this.dataForm.stairMess = data.house.stairMess
                this.dataForm.gasStatus = data.house.gasStatus
                this.dataForm.electricity = data.house.electricity
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/house/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'floorId': this.dataForm.floorId,
                'buildingId': this.dataForm.buildingId,
                'ownerId': this.dataForm.ownerId,
                'safetyInspectorId': this.dataForm.safetyInspectorId,
                'address': this.dataForm.address,
                'fireExtinguisherStatus': this.dataForm.fireExtinguisherStatus,
                'smokeDetectorStatus': this.dataForm.smokeDetectorStatus,
                'stairMess': this.dataForm.stairMess,
                'gasStatus': this.dataForm.gasStatus,
                'electricity': this.dataForm.electricity
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
